<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\goods\GoodsNavbarModel;
use think\Request;

/**
 * GoodsType 商品导航分类
 * Class GoodsType
 * @package app\controller
 */
class GoodsNavbar extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new GoodsNavbarModel();
    }


    /**
     * 分页列表
     * @param string $keyword
     * @param string $special
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPage($keyword = '', $special = '-1', $page = 1, $limit = 10)
    {
        $map = [];
        if (intval($special) != -1) {
            $map[] = ['special', '=', $special];
        }
        $data = $this->model->getPageData($map, $keyword, $page, $limit);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 获取单条数据
     * @param string $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id = '')
    {
        $data = $this->model->getInfo($id);
        return self::returnMsg(1, $data);
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'name' => $request->param('name'),
            'url' => $request->param('url'),
            'sort' => $request->param('sort'),
            'icon' => $request->param('icon'),
            "special" => $request->param("special"),//是否是敏感
            'type' => $request->param('type'),
            'order' => $request->param('order'),
            'status' => $request->param('status'),
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\GoodsNavbarValidate.add';
        } else {
            $validate = 'app\validate\GoodsNavbarValidate';
        }
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);//修改是否添加进缓存。。。
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }


    /**
     * 上线状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'status' => $request->param('status'),
        ];
        if (is_array($formData['id'])) {
            $list = [];
            foreach ($formData['id'] as $key => $value) {
                $list[] = ['id' => $value, 'status' => $formData['status']];
            }
            $ret = $this->model->saveAll($list);
            if ($ret) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        } else {
            $validate = 'app\validate\GoodsNavbarValidate.upstatus';
            $result = $this->validate($formData, $validate);
            if (true !== $result) {
                return self::returnMsg(991, [], $result);
            } else {
                $ret = $this->model->saveData($formData, false, false);
                if (false !== $ret['flag']) {
                    return self::returnMsg(1);
                } else {
                    return self::returnMsg(0);
                }
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'GoodsNavbarModel', '商品导航分类管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'goods');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }
}
